package com.dk.controller;

import com.dk.pojo.Permissions;
import com.dk.pojo.UUser;
import com.dk.service.PermissionsServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("permissionsController")
public class PermissionsController {

    @Autowired
    private PermissionsServiceI permissionsServiceI;

    //跳转查询页
    @RequestMapping("list")
    public String listGet(){
        return "Permissions/list";
    }

    //查询权限 树
    @RequestMapping("getTSource")
    @ResponseBody
    public List<Map<String,Object>> getTSource(HttpServletRequest request){
        //域对象获取登录成功用户实体类
        UUser uUser = (UUser)request.getSession().getAttribute("tUser");
        List<Map<String,Object>> list = permissionsServiceI.getTSource(uUser);
        return list;
    }

    //跳转ztree
    @RequestMapping("listGet")
    public String listZtree(){

        return "Permissions/showTree";
    }

    //查询并返回ztree
    @RequestMapping("getzTree")
    @ResponseBody
    public List<Map<String,Object>> getzTree(){
        List<Map<String,Object>> list =  permissionsServiceI.getzTree();
        return list;
    }

    //跳转ZTree增加页面
    @RequestMapping("addInfo")
    public String addInfo(){
        return "Permissions/add";
    }

    //ZTree确认增加
    @RequestMapping("addInfos")
    @ResponseBody
    public int addInfos(Permissions permissions,HttpServletRequest request){
        //域对象获取登录成功用户实体类
        UUser uUser = (UUser)request.getSession().getAttribute("tUser");
        permissionsServiceI.addInfos(permissions,uUser);
        return 1;
    }

    //跳转ZTree修改页面
    @RequestMapping("getUpdateInfo/{feId}")
    public String getUpdateInfo(@PathVariable("feId")Integer feId, Model model,HttpServletRequest request){
        UUser uUser = (UUser)request.getSession().getAttribute("tUser");
        Permissions permissions =  permissionsServiceI.getInfo(feId,uUser);
        model.addAttribute("p",permissions);
        return "Permissions/update";
    }

    //确认修改
    @RequestMapping("updateInfo")
    @ResponseBody
    public int updateInfo(Permissions permissions,HttpServletRequest request){
        //域对象获取登录成功用户实体类
        UUser uUser = (UUser)request.getSession().getAttribute("tUser");
        permissionsServiceI.updateInfo(permissions,uUser);
        return 1;
    }

    //ZTree删除
    @RequestMapping("deleteInfo/{feId}")
    @ResponseBody
    public int deleteInfo(@PathVariable("feId")Integer feId,HttpServletRequest request){
        //域对象获取登录成功用户实体类
        UUser uUser = (UUser)request.getSession().getAttribute("tUser");
        permissionsServiceI.deleteInfo(feId,uUser);
        return 1;
    }

    //根据角色Id 查询对应的权限
    @RequestMapping("getzCheckedTree/{rid}")
    @ResponseBody
    public List<Map<String,Object>> getzCheckedTree(@PathVariable("rid")Integer rid){
        List<Map<String,Object>> list = permissionsServiceI.getzCheckedTree(rid);
        return list;
    }
}
